
using System;
using System.Collections;
using System.Data;
using System.Xml;

using OR.Lazarus.Business;
using Notation = OR.Lazarus.Business.Entities.Notation;

using OR.Lazarus.Config;

namespace OR.Lazarus.Library.DataTier.Source
{
   public class FieldCollection : OR.Lazarus.Library.DataTier.Source.Lazarus.FieldCollection
   {
		public override void Add( FieldDetail pSource )
		{
			if ( pSource.IsPrimaryKey )
			{
				base.AddAt( 0, pSource );
			}
			else
			{
				base.Add ( pSource );
			}
		}

		public FieldDetail GetPKField()
		{
			FieldDetail			v_oReturn		= null;
			IndexCollection	v_oPKIndexes	= null;

			foreach ( FieldDetail v_oCurrent in this.List )
			{
				v_oPKIndexes	= v_oCurrent.Indexes.GetByType( CompilerSettings.INDEXTYPE_PRIMARY );

				switch ( v_oPKIndexes.Count )
				{
					case 0:
					{
						break;
					}
					case 1:
					{
						if ( v_oReturn != null )
						{
							throw new Exception( "Multiple PK indexes were found." );
						}

						v_oReturn = v_oCurrent;

						break;
					}
					default:
					{
						throw new Exception( "Multiple PK indexes were found." );
					}
				}
			}

			return ( v_oReturn );
		}

		public void Initialize( Notation.NotationDetail pNotation )
		{
			this.OnInitialize( pNotation );
		}

		protected virtual void OnInitialize( Notation.NotationDetail pNotation )
		{
			this.OnInitializeFields( pNotation );
		}

		protected virtual void OnInitializeFields( Notation.NotationDetail pNotation )
		{
			foreach ( FieldDetail vField in this.List )
			{
				vField.Initialize( pNotation );
			}
		}
	}
}
   